package com.service.admin.conversion.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.admin.conversion.controller.rep.ConvertPageRep;
import com.service.admin.conversion.controller.req.ConvertPageReq;
import com.service.admin.conversion.service.ConvertCoinsRecordService;
import com.service.core.rest.response.CommonPageRep;
import com.service.dao.mapper.ConversionCoinsRecordMapper;
import com.service.dao.model.ConvertCoinsRecord;
import com.service.utility.modelmapper.ModelMapperUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;


@Service("conversionCoinsRecordService")
public class ConvertCoinsRecordServiceImpl extends ServiceImpl<ConversionCoinsRecordMapper, ConvertCoinsRecord> implements ConvertCoinsRecordService {

    @Override
    public CommonPageRep<ConvertPageRep> pageRecord(ConvertPageReq pageReq) {
        Page<ConvertCoinsRecord> page = PageHelper.startPage(pageReq.getPageNo(), pageReq.getPageSize());
        LambdaQueryWrapper<ConvertCoinsRecord> queryWrapper = new QueryWrapper<ConvertCoinsRecord>().lambda()
                .eq(ConvertCoinsRecord::getTargetCoinsId, pageReq.getCoin().getCode());
        if (StringUtils.isNoneBlank(pageReq.getMemberAccount())){
            queryWrapper.like(ConvertCoinsRecord::getMemberAccount, pageReq.getMemberAccount());
        }

        if(StringUtils.isNoneBlank(pageReq.getStartTime())){
            queryWrapper.ge(ConvertCoinsRecord::getCreateTime, pageReq.getStartTime());
        }

        if(StringUtils.isNoneBlank(pageReq.getEndTime())){
            queryWrapper.le(ConvertCoinsRecord::getCreateTime, pageReq.getEndTime());
        }
        queryWrapper.orderByDesc(ConvertCoinsRecord::getCreateTime);

        list(queryWrapper);

        return CommonPageRep.buildResultRep(page, page.getResult().stream()
                .map(item -> ModelMapperUtil.map(item, ConvertPageRep.class)).collect(Collectors.toList()));
    }



}
